package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.ICardEntityService;
import com.younion.business.IFavoritesService;
import com.younion.business.IFavourableEntityService;
import com.younion.model.FavoritesOperateResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.FavoritesVO;

public class FavoritesAddConsumer extends AbstractRequestConsumer {
	private IFavoritesService favoritesService;
	private ICardEntityService cardEntityService;
	private IFavourableEntityService favourableEntityService;

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return addFavorites(request, response);
	}

	public String addFavorites(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String favourId = request.getParameter("favour_id");
		String type = request.getParameter("type");
		ResultObject resultObject = new ResultObject();
		FavoritesOperateResult result = new FavoritesOperateResult();
		try {
			Map countParamMap = new HashMap();
			countParamMap.put("userToken", user_token);
			countParamMap.put("favourId", favourId);
			countParamMap.put("type", type);
			int favourCount = favoritesService.getCountByMap(countParamMap);
			if (favourCount <= 0) {
				FavoritesVO favoritesVO = new FavoritesVO();
				favoritesVO.setCreateTime(new Date());
				favoritesVO.setFavourId(Integer.parseInt(favourId));
				favoritesVO.setType(Integer.parseInt(type));
				favoritesVO.setUserToken(user_token);
				favoritesService.save(favoritesVO);
				Map updateFavourAmountMap = new HashMap();
				updateFavourAmountMap.put("id", favourId);
				updateFavourAmountMap.put("amount", 1);
				if ("1".equals(type)) {
					cardEntityService
							.updateFavourAmountByMap(updateFavourAmountMap);
				} else if ("2".equals(type)) {
					favourableEntityService
							.updateFavourAmountByMap(updateFavourAmountMap);
				}
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				result.setSuccessful("1");
				resultObject.setResult(result);
				resultObject.setMsg(ApiConstant.FAVORITES_ADD_SUCCESS);
			} else {
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				result.setSuccessful("0");
				resultObject.setResult(result);
				resultObject.setMsg(ApiConstant.FAVORITES_EXISTS_ADD);
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			result.setSuccessful("0");
			resultObject.setResult(result);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}

}
